package org.example.serviceEdu.controller.front;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.example.ResultCode.JwtUtils;
import org.example.ResultCode.R;
import org.example.ResultCode.ordervo.CourseWebVoOrder;
import org.example.ResultCode.ordervo.UcenterMemberOrder;
import org.example.serviceEdu.client.OrdersClient;
import org.example.serviceEdu.entity.EduCourse;
import org.example.serviceEdu.entity.chapter.ChapterVo;
import org.example.serviceEdu.entity.frontvo.CourseFrontVo;
import org.example.serviceEdu.entity.frontvo.CourseWebVo;
import org.example.serviceEdu.service.EduChapterService;
import org.example.serviceEdu.service.EduCourseService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/serviceEdu/coursefront")
@CrossOrigin
public class CourseFrontController {
    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduChapterService chapterService;
    @Autowired
    private OrdersClient ordersClient;


    @PostMapping("getFrontCourseList/{page}/{limit}")
            public R getFrontCourseList(@PathVariable long page,
                                        @PathVariable long limit,
            @RequestBody(required=false) CourseFrontVo courseFrontVo){//false表示条件可以为空，不拨错
        //课程条件查询，带分页
        Page<EduCourse> pageCourse=new Page<>(page,limit);
        Map<String,Object> map=courseService.getCourseFrontList(pageCourse, courseFrontVo);
        return R.ok().data(map);
    }
    //2.查询课程详情
    @GetMapping("getFrontCourseInfo/{courseId}")
    public R getFrontCourseInfo(@PathVariable String courseId, HttpServletRequest request){
        CourseWebVo courseWebVo=courseService.getBaseCourseInfo(courseId);
        List<ChapterVo> chapterVideoList=chapterService.getChapterVideoByCourseId(courseId);
        //远程调用课程的status的值是否为1
        String memberId= JwtUtils.getMemberIdByJwtToken(request);
        boolean buyCourse=ordersClient.isBuyCourse(courseId,memberId);

        return  R.ok().data("courseWebVo",courseWebVo).data("chapterVideoList",chapterVideoList).data("isBuy",buyCourse);

    }
    //根据课程id获取课程信息(该方法供远程调用使用)
    @PostMapping("getCourseInfoOrder/{id}")
    public CourseWebVoOrder getCourseInfoOrder(@PathVariable String id){
        CourseWebVo courseInfo=courseService.getBaseCourseInfo(id);
        //把courseInfo对象里面的值复制给courseWebVoOrder对象
        CourseWebVoOrder courseWebVoOrder=new CourseWebVoOrder();
        BeanUtils.copyProperties(courseInfo,courseWebVoOrder);
        return  courseWebVoOrder;



    }
}
